<template>
  <div class="info-container">
    <el-card style="height: 100%">
      <search-tool @onSearch="handleSearch"></search-tool>
      <div>
        <el-button @click="handleAdd" type="primary">添加</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%; position: relative; margin-top: 20px"
        height="75%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="序号" type="index" />
        <el-table-column prop="userName" label="姓名"> </el-table-column>
        <el-table-column prop="reason" label="入廊事由	"> </el-table-column>
        <el-table-column prop="startTime" label="进廊时间"> </el-table-column>
        <el-table-column prop="endTime" label="出廊时间"> </el-table-column>
        <el-table-column prop="workUser" label="当班人员"> </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <div class="action-row">
              <button
                :class="!row.endTime === true ? 'Regbtn' : 'DeRegbtn'"
                :disabled="!row.endTime === true ? false : true"
                @click="handleRegist(row)"
              >
                登记
              </button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 20]"
        :page-size="size"
        layout="prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <add-dialog
      :title="dialogTitle"
      :rowData="curRowData"
      v-model:visible="visibleDialog"
      @onReload="reloadPage"
    ></add-dialog>
    <register-diaog
      :title="dialogRegTitle"
      :rowData="curRowDataReg"
      v-model:visible="visibleDialogRef"
      @onReload="reloadPageReg"
    ></register-diaog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import SearchTool from './components/SearchTool/index.vue'
import AddDialog from './components/AddDialog/index.vue'
import registerDiaog from './components/registerDialog/index.vue'
import { ElMessageBox } from 'element-plus'
import { ListDutyManagement } from '@/api/dutyManagement'
import { message } from '@/utils/message'
//数据相关
const tableData = ref([])
const total = ref(0)
const page = ref(1)
const size = ref(8)
const params = ref({})
const loading = ref(false)
const visibleDialog = ref(false)
const visibleDialogRef = ref(false)
const curRowData = ref({})
const curRowDataReg = ref({})
const dialogTitle = ref('')
const dialogRegTitle = ref('')

// 获取数据的方法
const getListData = async (params) => {
  loading.value = true
  const result = await ListDutyManagement(params, page.value, size.value)
  loading.value = false
  tableData.value = result.rows
  total.value = result.total
}
const reloadPage = () => {
  getListData(params.value)
  if (visibleDialog.value) {
    visibleDialog.value = false
  }
}
reloadPage()
const reloadPageReg = () => {
  getListData(params.value)
  if (visibleDialogRef.value) {
    visibleDialogRef.value = false
  }
}
reloadPageReg()
//查询条件相关
const handleSearch = (paramsData) => {
  params.value = paramsData
  let obj = {
    userName: paramsData.userName,
    startTime: paramsData.time[0],
    endTime: paramsData.time[1]
  }
  getListData(obj)
}
// 分页相关
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getListData(params.value)
}

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getListData(params.value)
}

const handleAdd = () => {
  dialogTitle.value = '新建入廊记录'
  curRowData.value = {}
  visibleDialog.value = true
}
const handleRegist = (row) => {
  dialogRegTitle.value = '新增出廊时间'
  curRowDataReg.value = row
  visibleDialogRef.value = true
}
</script>

<style lang="scss" scoped>
.info-container {
  width: 100%;
  height: 100%;
  .Regbtn {
    width: 53px;
    height: 30px;
    background: #afffe8;
    border-radius: 6px 6px 6px 6px;
    opacity: 1;
    border: 1px solid #00c38b;
    font-size: 14px;
    color: #00c38b;
    &:hover {
      cursor: pointer;
    }
  }
  .DeRegbtn {
    width: 53px;
    height: 30px;
    background: #f1f1f1;
    border-radius: 6px 6px 6px 6px;
    opacity: 1;
    border: 1px solid #aaaaaa;
    font-size: 14px;
    color: #b5b5b5;
  }

  .action-row {
    display: flex;
  }
  .pagination {
    width: 100%;
    height: 32px;
    margin-top: 20px;
    display: flex;
    align-items: center;
    justify-content: end;
  }
}
</style>
